app.controller('materialscontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http)
{
	$scope.$on('translations', function(event, args) {
		console.info('Material translations received');
		$scope.translations = args.translations.response;
		$scope.getMaterialStatesCommand();
	});

	$scope.$on('materialStatesReceived', function(event, args) {
		console.info('Material states received');
		$scope.getMaterialsCommand();
	});

	/**
	* Add material command
	*/
	$scope.addMaterialCommand = function()
	{
		try
		{
			console.info("addMaterialCommand - called"); 
			$scope.canAddMaterial = false;
			
			if($scope.addMaterial_Name == "" || $scope.addMaterial_Name == undefined)
			{
				throw "Le nom du matériel est obligatoire.";
			}
			
			if($scope.addMaterial_State == "")
			{
				throw "L'état du matériel est obligatoire.";
			}
			
			var affectedUserId = null;
			
			if($scope.addMaterial_AffectedUser != undefined)
			{
				affectedUserId = $scope.addMaterial_AffectedUser.Id;
			}
			
			var state = $scope.materialsState.indexOf($scope.addMaterial_State);

			$http.post($scope.servicebase, { "service" : "addMaterial", "state" : state, "name" : $scope.addMaterial_Name, "description" : $scope.addMaterial_Description, "affectedUser" : affectedUserId, "location" : $scope.addMaterial_Location }).
			success(function(data, status) 
			{
				console.info("addMaterialCommand - call success"); 
				$scope.status = status;
				$scope.data = data;
				$scope.result = data; 
				$scope.loadingArticles = false;
				
				if(data.isFailed == true)
				{
					$scope.setMessageCommand($scope.data.exception, 'danger');
				}
				
				$scope.messageAddMaterialVisible = true;
				$scope.setMessageCommand('Matériel ajouté en base de données', 'success', 'messageAddMaterial');
				$scope.materials.push($scope.data.response);
				$scope.canAddMaterial = true;
				$("#addMaterial").modal('hide');
			})
			.
			error(function(data, status) 
			{
				console.error("addMaterialCommand - call failed"); 	
				$scope.canAddMaterial = true;
				$scope.data = data || "Request failed";
				$scope.status = status;			
				console.error("addMaterialCommand - data : " + $scope.data); 				
			});
		}
		catch(err) 
		{
			$scope.messageAddMaterialVisible = true;
			$scope.canAddMaterial = true;
			$scope.setMessageCommand(err, 'danger', 'messageAddMaterial');
		}
	};
	
	/**
	* Prepare to add a material
	*/
	$scope.prepareAddMaterialCommand = function()
	{
		$("#addMaterial").modal('show');
	};
	
	/**
	* Prepare to edit the material
	*/
	$scope.prepareEditMaterialCommand = function(material)
	{
		console.info("Set all material informations");
		
		$scope.canUpdateMaterial = false;
		$scope.editMaterial_Id = material.Id;
		$scope.editMaterial_Name = material.Name;
		$scope.editMaterial_Description = material.Description;

		$scope.users.forEach(function(user) 
		{
			if(user.Id == material.AffectedUser.Id)
			{
				$scope.editMaterial_AffectedUser = user;
			}
		});
		
		$scope.editMaterial_Location = material.Location;
		$scope.editMaterial_State = $scope.materialsState[material.State];
		$("#editMaterial").modal('show');
		$scope.canUpdateMaterial = true;
	};
	
	/**
	* Update a material
	*/
	$scope.updateMaterialCommand = function()
	{
		try
		{
			console.info("updateMaterialCommand - called"); 
			$scope.canUpdateMaterial = false;
			
			if($scope.editMaterial_Name == "" || $scope.editMaterial_Name == undefined)
			{
				throw "Le nom du matériel est obligatoire.";
			}
			
			if($scope.editMaterial_State == "")
			{
				throw "L'état du matériel est obligatoire.";
			}
			
			var affectedUserId = -1;
		
			if($scope.editMaterial_AffectedUser != 0 && $scope.editMaterial_AffectedUser != -1)
			{
				affectedUserId = $scope.editMaterial_AffectedUser.Id;
			}
			
			var state = $scope.materialsState.indexOf($scope.editMaterial_State);

			$http.post($scope.servicebase, { "service" : "updateMaterial", "id" : $scope.editMaterial_Id, "state" : state, "name" : $scope.editMaterial_Name, "description" : $scope.editMaterial_Description, "affectedUser" : affectedUserId, "location" : $scope.editMaterial_Location }).
			success(function(data, status) 
			{
				console.info("updateMaterialCommand - call success"); 
				$scope.status = status;
				$scope.data = data;
				$scope.result = data; 

				if(data.isFailed == true)
				{
					$scope.setMessageCommand($scope.data.exception, 'danger');
				}
				
				$scope.messageUpdateMaterialVisible = true;
				$scope.setMessageCommand('Matériel mis a jour en base de données', 'success', 'messageUpdateMaterial');
				$scope.getMaterialsCommand();
				$scope.canUpdateMaterial = true;
			})
			.
			error(function(data, status) 
			{
				console.error("updateMaterialCommand - call failed"); 	
				$scope.canUpdateMaterial = true;
				$scope.data = data || "Request failed";
				$scope.status = status;			
				console.error("updateMaterialCommand - data : " + $scope.data); 				
			});
		}
		catch(err) 
		{
			$scope.messageUpdateMaterialVisible = true;
			$scope.canUpdateMaterial = true;
			$scope.setMessageCommand(err, 'danger', 'messageUpdateMaterial');
		}
	};
	
	/**
	* Delete the selected material
	*/
	$scope.deleteMaterialCommand = function(material)
	{
		console.info("deleteMaterialCommand - called"); 
		$http.post($scope.servicebase, { "service" : "deleteMaterial", "id" : material.Id }).
		success(function(data, status) 
		{
			console.info("deleteMaterialCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			var index = $scope.materials.indexOf(material);
			if (index > -1) 
			{
				$scope.materials.splice(index, 1);
			}
			$("#confirmDeleteMaterial").modal('hide');
		})
		.
		error(function(data, status) 
		{
			console.error("deleteMaterialCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("deleteMaterialCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Confirm to delete the material
	*/
	$scope.confirmDeleteMaterialCommand = function(material)
	{
		$scope.currentMaterial = material;
		$("#confirmDeleteMaterial").modal('show');
	};
	
	/**
	* Cancel the event deletion
	*/
	$scope.cancelDeleteMaterialCommand = function()
	{
		$("#confirmDeleteMaterial").modal('hide');
	};
		
	/**
	* Get all material states
	*/
	$scope.getMaterialStatesCommand = function()
	{
		console.info("getMaterialStatesCommand - called");
		
		$scope.canAddMaterial = false;
		
		$http.post($scope.servicebase, { "service" : "getMaterialStates"}).
		success(function(data, status) 
		{
			console.info("getProfiles - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				for(var materialstate in $scope.data.response) {
				   $scope.materialsState.push($scope.translations["Materials_State_" + materialstate]);
				}
			}

			$scope.$broadcast('materialStatesReceived', {
				states: $scope.materialsState 
			});
			
			$scope.canAddMaterial = true;
		})
		.
		error(function(data, status) 
		{
			console.error("getMaterialStatesCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.canAddMaterial = true;
			$scope.status = status;
			console.error("getMaterialStatesCommand - data : " + $scope.data);		
		});
	};
	
	/**
	* Get all users
	*/
	$scope.getAllMembersCommand = function()
	{
		console.info("getAllMembersCommand - called"); 
		$http.post($scope.servicebase, { "service" : "getUsersMemberMinimum"}).
		success(function(data, status) 
		{
			console.info("getMaterialsCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.users = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getMaterialsCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getMaterialsCommand - data : " + $scope.data); 				
		});
	}
	
	/**
	* Get all materials
	*/
	$scope.getMaterialsCommand = function()
	{
		console.info("getMaterialsCommand - called"); 
		$scope.loadingMaterials = true;
		
		$http.post($scope.servicebase, { "service" : "getMaterials"}).
		success(function(data, status) 
		{
			console.info("getMaterialsCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingMaterials = false;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.materials = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getMaterialsCommand - call failed"); 	
			$scope.loadingMaterials = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getMaterialsCommand - data : " + $scope.data); 				
		});
	};
			
	/**
	* Load the material component
	*/
	$scope.loadMaterialsComponentCommand = function() 
	{
		console.info("loadMaterialsComponentCommand - called");
		$scope.getAllMembersCommand();
	};
	
	$scope.loadMaterialsComponentCommand();
	
}]);